/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_FLV_DEMUXER_H
#define MK_FLV_DEMUXER_H
#include "MkAmf/MkAmf.h"
#include "MkMedia/MkMediaUtil.h"

class MkApi CMkFlvDemuxer
{
public:
    CMkFlvDemuxer();
    ~CMkFlvDemuxer();

    Uint32 InputData(Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 GetEsPacket(MkEsPacket &EsPacket);
    const CMkVideoParam& GetVideoParam() const { return m_VideoParam; }
    const CMkAudioParam& GetAudioParam() const { return m_AudioParam; }
    const Uint32& GetFileSize() const { return m_FileSize; }
    const Uint32& GetDuration() const { return m_Duration; }
    static Uint32 AmfObjectParse(const MkVector<MkAmfObject>& VecObject, Uint32& Width, Uint32& Height, Uint32& VideoBitRate, Uint32& FrameRate, Uint8& VideoCodecType, Uint8& AudioCodeType, Uint32& AudioBitRate, Uint32& AudioSampleRate, Uint32& AudioSampleSize, BOOL& bStereo, Uint32& Duration, Uint32& FileSize);
    static Uint32 AudioTagParse(const Uint8 *pBuf, Uint32 nLen, Uint32 TimeStamp, BOOL Encrypted, CMkAudioParam& AudioParam, MkEsPacket& EsPacket);
    static Uint32 VideoTagParse(const Uint8 *pBuf, Uint32 nLen, Uint32 TimeStamp, BOOL Encrypted, CMkVideoParam& VideoParam, MkEsPacket& EsPacket);
protected:
private:
    Uint32 ParseHeader(Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParseTag(Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ScriptTagParse(Uint8 *pBuf, Uint32 nLen, BOOL Encrypted);
private:
    MkQueue<MkEsPacket> m_EsQueue;
    CMkVideoParam   m_VideoParam;
    CMkAudioParam   m_AudioParam;
    BOOL            m_GetFlvHeader;
    BOOL            m_HasVideo;
    BOOL            m_HasAudio;
    Uint32          m_FileSize;
    Uint32          m_Duration;
};


#endif